package com.hupu.jrs.components.retrofit

import com.alibaba.fastjson.JSON
import okhttp3.MediaType
import okhttp3.RequestBody
import retrofit2.Converter
import java.io.IOException
import java.lang.reflect.Type
import java.nio.charset.Charset

/**
 * Created by sll on 2016/3/31.
 */
class FastJsonRequestBodyConverter<T>(private val type: Type, private val charset: Charset) : Converter<T, RequestBody> {

    @Throws(IOException::class)
    override fun convert(value: T): RequestBody {
        return RequestBody.create(MEDIA_TYPE, JSON.toJSONString(value).toByteArray(charset))
    }

    companion object {
        private val MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8")
    }
}
